<template>
  <div class="app">
    <div class="search-bar">
      <el-form :model="searchData" label-width="90px">
        <el-row :gutter="5">
          <el-col :span="6">
            <el-form-item label="预警日期" style="margin-bottom: 5px;">
              <el-date-picker clearable class="select-item" v-model="searchData.calculateDate" type="date" format="yyyy 年 MM 月 dd 日" size="medium" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="预警公司" style="margin-bottom: 5px;">
              <el-select v-model="searchData.alertCompany"
                size="medium"
                class="select-item"
                clearable
                filterable
                :filter-method="filterMethod"
                @visible-change="visibleChange"
                v-el-select-loadmore="loadMore(rangeNumber)"
                placeholder="请选择公司">
                <el-option
                  v-for="item in options.slice(0, rangeNumber)"
                  :key="item.code"
                  :label="item.value"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="预警结果" style="margin-bottom: 5px;">
              <el-select
                class="select-item"
                size="medium"
                v-model="searchData.alertResult"
                clearable
                placeholder="请选择">
                <el-option label="通过" value="1"></el-option>
                <el-option label="不通过" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <div style="margin-top: 3px; display: flex; justify-content: center">
              <el-button icon="el-icon-search" type="primary" size="small" @click="handleSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" size="small" @click="handleClear">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div ref="ref" class="table-box">
      <TableComponent :columns="tableColumns" :height="height" :data="tableData" @change="onTableChange" :tableOption="tableOption" :page-option="searchData.page" :stripe="stripe" :total="total">
      </TableComponent>
    </div>
  </div>
</template>

<script>
import TableComponent from '@/components/Table/table'
import { queryWarningDetail, queryAllCompanyList } from '@/api/modules/warning.js'
import { tableColumns, computeOption } from './index.js'
import { _debounce } from './index.js'
export default {
  components: {
    TableComponent
  },
  data() {
    return {
      searchData: {
        calculateDate: '',
        alertCompany: '',
        alertResult: '',
        page: {
          pageNum: 1,
          pageSize: 10
        }
      },
      tableColumns,
      computeOption,
      tableData: [],
      height: null,
      title: '',
      total: 0,
      tableOption: {
        isShowOperate: false
      },
      stripe: true,
      options: [],
      companyNameOption: [],
      rangeNumber: 10
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.height = this.$refs.ref.offsetHeight
    })
    window.onresize = () => {
      this.height = this.$refs.ref.offsetHeight
    }
    this.initData(this.searchData)
    queryAllCompanyList({ type: '1', code: '', chiName: '' }).then(res => {
      if (res.data.code === '0') {
        this.companyNameOption = res.data.body
      }
    })
  },
  destroyed(){
    window.onresize = null
  },
  directives:{
    'el-select-loadmore':(el, binding) => {
      // 获取element-ui定义好的scroll盒子
      const SELECTWRAP_DOM = el.querySelector(".el-select-dropdown .el-select-dropdown__wrap")
      if(SELECTWRAP_DOM){
        SELECTWRAP_DOM.addEventListener("scroll", function () {
          /**
           * scrollHeight 获取元素内容高度(只读)
           * scrollTop 获取或者设置元素的偏移值,
           *  常用于:计算滚动条的位置, 当一个元素的容器没有产生垂直方向的滚动条, 那它的scrollTop的值默认为0.
           * clientHeight 读取元素的可见高度(只读)
           * 如果元素滚动到底, 下面等式返回true, 没有则返回false:
           * ele.scrollHeight - ele.scrollTop === ele.clientHeight;
           */
          const condition = this.scrollHeight - this.scrollTop <= this.clientHeight
          if (condition) binding.value()
        })
      }
    }
  },
  methods: {
    initData(params) {
      queryWarningDetail(params).then(res => {
        if (res.data.code === '200') {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        }
      })
    },
    // 点击查询
    handleSearch() {
      this.initData(this.searchData)
    },
    // 分页
    onTableChange(val) {
      this.searchData.page = val
      this.initData(this.searchData)
    },
    handleClear() {
      this.searchData.calculateDate = ''
      this.searchData.alertCompany = ''
      this.searchData.cmhkId = ''
      this.searchData.alertResult = ''
    },
    // 筛选方法
    filterMethod:_debounce(function(filterVal) {
      if(filterVal){
        let filterArr = this.companyNameOption.filter((item) => {
          return item.value.toLowerCase().includes(filterVal.toLowerCase())
        })
        this.options = filterArr
      }else{
        this.options = this.companyNameOption
      }
    }),
    // 下拉框出现时，调用过滤方法
    visibleChange(flag){
      if(flag){
        this.filterMethod()
      }
    },
    loadMore(n) {
      // n是默认初始展示的条数会在渲染的时候就可以获取,具体可以打log查看
      // elementui下拉超过7条才会出滚动条,如果初始不出滚动条无法触发loadMore方法
      return () => (this.rangeNumber += 5) // 每次滚动到底部可以新增条数  可自定义
    }
  }
}
</script>

<style lang="less" scoped>
  .app {
    height: 100%;
    .search-bar {
      width: calc(100% - 60px);
      background-color: #ffffff;
      margin-bottom: 10px;
      padding: 10px 30px;
    }
    .add-form {
      padding: 10px 20px;
      background-color: #ffffff;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 200px);
      .table {
        height: 100%;
      }
    }
    .select-item {
      width: 100%;
    }
  }
</style>
